#!/bin/bash
# Author: liaojunhua 
# QQ: 563293401
#/RockerMQ 2M-2S-ASYNC Start, Stop, Restart

# Source function library.
. /etc/init.d/functions

# Add Java env
export JAVA_HOME=/usr/local/java
export JRE_HOME=${JAVA_HOME}/jre
export PATH=${JAVA_HOME}/bin:$PATH
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib

RMQ_ROLE=$1
RMQ_DIR="{{ work_dir }}/{{ mq_un_name }}"
RMQ_BIN="${RMQ_DIR}/bin"
RMQ_CONF="${RMQ_DIR}/conf/2m-2s-async"

ROLE_NAME_CONF=$(ls $RMQ_CONF |grep "properties$")
ROLE_NAME=$(ls $RMQ_CONF |grep "properties$" |awk -F'.' '{print $1}')
ROLE_NAME_V=$(eval echo $ROLE_NAME)
#broker and Namesrv only one
ROLE_ALL="$ROLE_NAME_CONF namesrv.properties"

RMQF_Help() {
    echo "Useage:: $0 ROLE(namesrv $ROLE_NAME_V) (start|stop|restart|log)"
    exit 1
}

RMQF_Check() {
    grep ${RMQ_ROLE}.properties <<< $ROLE_ALL >/dev/null 2>&1
    if [[ $? != 0 ]]; then
        echo "Useage:: $0 ROLE(namesrv $ROLE_NAME_V) (start|stop|restart|log)"
        exit 1
    fi
}

RMQF_Start() {
    if [[ $RMQ_ROLE == "namesrv" ]]; then
        nohup sh ${RMQ_BIN}/mqnamesrv >>${RMQ_DIR}/logs/${RMQ_ROLE}.log 2>&1 &
        echo "logs: ${RMQ_DIR}/logs/${RMQ_ROLE}.log"
        action "MQ NameServer is start" /bin/true
    else
        nohup sh ${RMQ_BIN}/mqbroker -c ${RMQ_CONF}/${RMQ_ROLE}.properties >>${RMQ_DIR}/logs/${RMQ_ROLE}.log 2>&1 &
        echo "logs: ${RMQ_DIR}/logs/${RMQ_ROLE}.log"
        action "Broker ${RMQ_ROLE} is start" /bin/true
    fi
}

RMQF_Stop() {
    if [[ $RMQ_ROLE == "namesrv" ]]; then
        NPID=`ps ax | grep -i 'org.apache.rocketmq.namesrv.NamesrvStartup' |grep java | grep -v grep | awk '{print $1}'`
        if [[ -n $NPID ]]; then
            kill -9 $NPID >/dev/null 2>&1
            CheckPid=`ps ax | grep -i 'org.apache.rocketmq.namesrv.NamesrvStartup' |grep java | grep -v grep | awk '{print $1}'`
            [[ -z $CheckPid ]] && action "MQ NameServer Stop..." /bin/true || action "MQ NameServer Stop..." /bin/false
        else
            action "MQ NameServer Don't Running..." /bin/false
        fi
    else
        BPID=`ps ax | grep -i 'org.apache.rocketmq.broker.BrokerStartup' |grep java |grep ${RMQ_ROLE}.properties |grep -v grep |awk '{print $1}'`
        if [[ -n $BPID ]]; then
            kill -9 $BPID >/dev/null 2>&1
            CheckPid=`ps ax | grep -i 'org.apache.rocketmq.broker.BrokerStartup' |grep java |grep ${RMQ_ROLE}.properties |grep -v grep |awk '{print $1}'`
            [[ -z $CheckPid ]] && action "${RMQ_ROLE} Stop..." /bin/true || action "${RMQ_ROLE} Stop..." /bin/false
        else
            action "${RMQ_ROLE} Don't Running..." /bin/false
        fi
    fi
}


RMQF_LOG() {
    tail -f ${RMQ_DIR}/logs/${RMQ_ROLE}.log -n 300
}

case "$2" in
    stop )
        [ $# -ne 2 ] && { RMQF_Help; exit; }
        RMQF_Check
        RMQF_Stop
        ;;

    start )
        [ $# -ne 2 ] && { RMQF_Help; exit; }
        RMQF_Check
        RMQF_Start
        ;;

    restart )
        [ $# -ne 2 ] && { RMQF_Help; exit; }
        RMQF_Check
        RMQF_Stop
        sleep 10
        RMQF_Start
        ;;

    log )
        [ $# -ne 2 ] && { RMQF_Help; exit; }
        RMQF_Check
        RMQF_LOG
        ;;

    * )
        RMQF_Help
        ;;
esac
exit 0
